package com.whut.whw.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.whut.whw.R;
import com.whut.whw.base.BaseActivity;
import com.whut.whw.model.BackResult;
import com.whut.whw.util.BackResultCallback;
import com.whut.whw.util.InputUtils;
import com.whut.whw.util.StringUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import butterknife.BindView;
import okhttp3.Call;

import static com.whut.whw.constant.ConfigConsts.REG_URL;

/**
 * Created by 张芳容 on 2017/06/21.
 */

public class RegisterActivity extends BaseActivity {
    @BindView(R.id.coordinatorLayout)
    CoordinatorLayout coordinatorLayout;

    //登录按钮
    @BindView(R.id.main_btn__login_register)
    TextView main_btn_login;
    //注册按钮
    @BindView(R.id.main_btn_register)
    TextView main_btn_register;
    //返回按钮
    @BindView(R.id.btn_back)
    ImageView btn_back;
    @BindView(R.id.register_username)
    EditText register_username;
    @BindView(R.id.register_password)
    EditText register_pwd;
    @BindView(R.id.register_rePassword)
    EditText register_repwd;

    //用户名
    String username;
    //密码
    String password;
    //重复密码
    String repassword;

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        main_btn_login.setText(R.string.login);
        //设置输入限制->不允许输入空格
        InputUtils.setEditTextInhibitInputSpace(register_username);
        InputUtils.setEditTextInhibitInputSpace(register_pwd);
        InputUtils.setEditTextInhibitInputSpace(register_repwd);
    }

    @Override
    public void setListener() {
        main_btn_login.setOnClickListener(new Login_listener());//前往登录界面
        main_btn_register.setOnClickListener(new Register_listener());//注册
        btn_back.setOnClickListener(new Back_listener());//监听返回事件
    }

    //登录
    private class Login_listener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            finish();
        }
    }

    private class Register_listener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (regCheck()) {
                OkHttpUtils
                        .post()
                        .url(REG_URL)
                        .addParams("name", username)
                        .addParams("password", password)
                        .build()
                        .execute(new BackResultCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                getUiDelegate().toastLong(e.toString());
                                Log.e("TC", e.toString());
                            }

                            @Override
                            public void onResponse(BackResult response, int id) {
                                //提示信息
                                Snackbar.make(coordinatorLayout, response.getMsg(), Snackbar.LENGTH_LONG).show();
                                //如果注册成功
                                if (response.isSuccess()) {
                                    //将注册成功的用户名存入xml中，并销毁页面回到登录页面
                                    SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
                                    SharedPreferences.Editor ed = sp.edit();
                                    ed.clear();
                                    ed.putString("nc", username);
                                    ed.commit();
                                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            }
                        });
            }
        }
    }

    /**
     * 注册验证
     *
     * @return
     */
    private boolean regCheck() {
        username = register_username.getText().toString().trim();
        password = register_pwd.getText().toString().trim();
        repassword = register_repwd.getText().toString().trim();
        if (StringUtils.isTrimEmpty(username)) {
            Snackbar.make(coordinatorLayout, getResources().getString(R.string.enter_username_ck), Snackbar.LENGTH_LONG).show();
            return false;
        }
        if (StringUtils.isTrimEmpty(password)) {
            Snackbar.make(coordinatorLayout, getResources().getString(R.string.enter_pwd_ck), Snackbar.LENGTH_LONG).show();
            return false;
        }
        if (password.length() < 6) {
            Snackbar.make(coordinatorLayout, getResources().getString(R.string.pwd_length_ck), Snackbar.LENGTH_LONG).show();
            return false;
        }
        if (!password.equals(repassword)) {
            Snackbar.make(coordinatorLayout, getResources().getString(R.string.pwd_again_ck), Snackbar.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    private class Back_listener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }
}
